package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;

import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.heima.utils.common.WmThreadLocalUtil.WM_USER_THREAD_LOCAL;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper,WmMaterial> implements WmMaterialService {

    @Autowired
    FileStorageService fileStorageService;

    @Autowired(required = false)
    WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult<?> uploadPicture(MultipartFile file) {
        if(file==null||file.getSize()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        try {
            InputStream inputStream = file.getInputStream();
            String replace = UUID.randomUUID().toString().replace("-", "");
            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String url = fileStorageService.uploadImgFile("", replace + substring, inputStream);
            WmUser wmUser = WM_USER_THREAD_LOCAL.get();
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(wmUser.getId());
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUrl(url);
            wmMaterial.setType((short) 0);
            save(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

    }

    @Override
    public ResponseResult findAll(WmMaterialDto dto) {
        if(dto.getIsCollection()==null){
           dto.setIsCollection((short) 0);
        }
        Page<WmMaterial> page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new QueryWrapper<WmMaterial>().lambda();
        if(dto.getIsCollection()==null&&dto.getIsCollection()==1){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //按用户查找
        lambdaQueryWrapper.eq(WmMaterial::getUserId,WM_USER_THREAD_LOCAL.get().getId()).orderByDesc(WmMaterial::getCreatedTime);
        Page<WmMaterial> page1=page(page,lambdaQueryWrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int) page1.getTotal());
        responseResult.setData(page1.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult collect(String id) {
        WmMaterial wmMaterial = baseMapper.selectById(id);
        //wmMaterial.setIsCollection((short) 1);
        if(wmMaterial.getIsCollection()==0){
            wmMaterial.setIsCollection((short) 1);
        }else {
            wmMaterial.setIsCollection((short) 0);
        }
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult delPicture(String id) {
        WmNewsMaterial wmNewsMaterial = wmNewsMaterialMapper.selectOne(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if(wmNewsMaterial!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该图片已被引用");
        }else {
            baseMapper.deleteById(id);
        }
        return ResponseResult.okResult(null);
    }


}
